<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/6/12
 * Time: 15:59
 */

namespace app\api\controller;

use think\Validate;
use think\Db;

class Customer extends Auto
{



//查询 会员 模糊
    protected $cusquery_rule =   [
        'id'=>'require',
        'content'  => 'require|max:25',
        'pageindex'  => 'number',
        'pagesize'  => 'number',
    ];
//查询 会员 模糊
    protected $cusquery_message  =   [
        'id.require' => 'id必填参数',
        'content.require' => '查询内容必填参数',
        'content.max'     => '查询内容最多不能超过25个字符',
        'pageindex.number'     => 'pageindex为int型',
        'pagesize.number'     => 'pagesize为int型',
    ];
//注册 会员
    protected $cus_register_rule =   [
        'id'=>'require',
        'cust_name'  => 'max:25',
        'org_code'  => 'require|max:25',
        'birth_year'  => 'number|max:4',
//        'train_target'  => 'string',
        'sex'  => 'max:1',
    ];
//注册 会员
    protected $cus_register_message  =   [
        'id.require' => 'id必填参数',
        'cust_name.max'     => '用户名最多不能超过25个字符',
        'org_code.require' => 'org_code必填参数',
        'org_code.max'     => '用户名最多不能超过25个字符',
        'birth_year.max'     => '出生年月最多不能超过4个字符',
        'birth_year.number'     => '出生年月为int类型',
//        'train_target.string'     => '目的为字符串类型',
        'sex.max'     => '性别最大不能超过1个字符',
    ];


    /**
     * @desc 注册
     * @return \think\response\Json
     */
    public function register(){

        $data = [
            'id'=>input('id'),
            'cust_name'=>input('cust_name'),
            'org_code'=>input('org_code'),
            'sex'=>input('sex'),
            'birth_year'=>input('birth_year'),
            'height'=>input('height'),
            'weight'=>input('weight'),
            'mobile'=>input('mobile'),
            'train_target'=>input('train_target'),
            'mark'=>1,
            'create_date'=>date('Y-m-d H:i:s',time()),
        ];

        $validate = new Validate($this->cus_register_rule,$this->cus_register_message);
        $result   = $validate->check($data);
        if(!$result){
            $message = $validate->getError();
            return json(returnRest($message,[],100001));
        }
        unset($data['id']);
        $resouce = $this->cusregistercheck($data);
        return json($resouce);
    }

    /**
     * @desc 核对登录
     * @param $data
     * @return array
     */
    public function cusregistercheck($data){
        try{
            $result  = Db::table('customer')->insert($data);
            $message ='会员添加成功！';
            return returnRest($message,[],0);
        } catch (\Exception $e){
            $message= $e->getMessage();
            return returnRest($message,[],9001);
        }
    }

    /**
     * @desc 模糊查询
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function query(){
        $data = [
            'id'=>input('id'),
            'content'=>input('keyword'),
            'pageindex'=>input('pageindex')?input('pageindex'):1,
            'pagesize'=>input('pagesize')?input('pagesize'):10,
        ];
        $validate = new Validate($this->cusquery_rule,$this->cusquery_message);
        $result   = $validate->check($data);
        if(!$result){
            $message = $validate->getError();
            return json(returnRest($message,[],100001));
        }
        $pageindex = ($data['pageindex']-1)*10;
        $pagesize = $data['pagesize'];
        $pagestr = $pageindex.','.$pagesize;
        $content =input('keyword');
        unset($data['id']);
        unset($data['pageindex']);
        unset($data['pagesize']);
        unset($data['content']);
        $result = Db::table('customer')
            ->field('cust_name,mobile,sex,birth_year,height,weight,train_target')
            ->where('cust_name','like','%'.$content.'%')
            ->whereOr('mobile','like','%'.$content.'%')
            ->where('org_code',input('org_code'))
            ->limit($pagestr)->select();
        $resultcount  = Db::table('customer')
            ->where('cust_name','like','%'.$content.'%')
            ->whereOr('mobile','like','%'.$content.'%')
            ->where('org_code',input('org_code'))
            ->count();
        $message ='';
        $content = [
            'pageindex'=>$pageindex+1,
            'pagesize'=>$pagesize,
            'pagecount'=>$resultcount,
            'content'=>$result
        ];
        return json(returnRest($message,$content,0));
    }



}